/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package ourMessage;

import ChateoController.Controller;
import ChateoController.FileTransfer;
import ChateoController.User;
import ChateoFacadeNI.*;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.ObjectInputStream;
import java.io.OutputStreamWriter;
import java.net.InetAddress;
import java.net.Socket;
import java.util.logging.Level;
import java.util.logging.Logger;

public class ConnexionTCPClient extends Thread {

    private static ConnexionTCPClient instance = null;
    private InetAddress addrClient;
    private int portClient;
    private Socket sockClient;
    private User localUser;
    private String myLogin;
    private FacadeNI facada;
    private Controller fileControl;
    private FileTransfer fileTransferState;
    private BufferedInputStream buffInStream;
    private ObjectInputStream objInStream;

    private ConnexionTCPClient(User localUser, Controller fileControl) {
        this.myLogin = localUser.getLogin();
        this.addrClient = localUser.getAddr();
        this.fileControl = fileControl;

        /**
         * ***** TODO: Create a getTransferPort() method in the Controller in
         * order to get the port that is gonna be used for files transfer
         ***************************************************************
         */
        //this.portClient = fileControl.getTransferPort();
    }

    public ConnexionTCPClient(InetAddress addr, int port, FacadeNI facada) {
        this.addrClient = addr;
        this.portClient = port;
        this.facada = facada;

        super.start();
    }

    /**
     *
     * @param fileControl
     * @param localUser
     * @return
     */
    public final static ConnexionTCPClient getInstance(Controller fileControl, User localUser) {
        synchronized (ConnexionTCPClient.class) {
            if (ConnexionTCPClient.instance == null) {
                ConnexionTCPClient.instance = new ConnexionTCPClient(localUser, fileControl);
            }
        }
        return ConnexionTCPClient.instance;
    }

    public final static void resetInstance() {
        ConnexionTCPClient.instance = null;
    }

    @Override
    public void run() {
        try {
            Message msg;
            this.sockClient = new Socket(localUser.getAddr(), portClient);
            this.buffInStream = new BufferedInputStream(sockClient.getInputStream());
            this.objInStream = new ObjectInputStream(this.buffInStream);

            while (fileTransferState.getStatus() == FileTransfer.ACCEPTED) {
                msg = (Message) objInStream.readObject();

                /**
                 * *TODO: Create in the Controller,a receivedMessage method for
                 * receiving files
                 */
                //fileControl.receivedMessage(new User(sockClient.getInetAddress(), msg.getlogin()));
            }

        } catch (IOException ex) {
            System.err.println("Erreur in : " + ConnexionTCPClient.class.getName().toString() + " : " + ex.getMessage());
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(ConnexionTCPClient.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /**
     *This method is aimed in closing the client socket and stream
     */
    public void close() {

        try {
            objInStream.close();
            buffInStream.close();
            sockClient.close();
        } catch (IOException ex) {
            Logger.getLogger(ConnexionTCPClient.class.getName()).log(Level.SEVERE, null, ex);
        }

    }

    /**
     * Getter for the reception File Transfer
     * @return 
     */
    public FileTransfer getFileTransferState() {
        return fileTransferState;
    }

    /**
     * Setter for the reception File Transfer
     * @param fileTransferState 
     */
    public void setFileTransferState(FileTransfer fileTransferState) {
        this.fileTransferState = fileTransferState;
    }
    
    

}
